package view.library
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import model.ModelLibraryConflicts;
	
	import mx.core.FlexGlobals;
	import mx.events.CollectionEvent;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class MediatorScreenLibrary extends Mediator
	{
		[Inject]
		public var v:ScreenLibrary;
		
		[Inject]
		public var mlc:ModelLibraryConflicts;
		
		private var popup:PopupLibraryConflicts;
		
		private var checkScheduled:Boolean = false;
		override public function onRegister() : void  {
			
			eventMap.mapListener(mlc.conflicImages, CollectionEvent.COLLECTION_CHANGE, checkLater);
			eventMap.mapListener(mlc.conflictBinaries, CollectionEvent.COLLECTION_CHANGE, checkLater);
		}
		
		private function check(e:Event = null) : void {
			checkScheduled = false;
			if (((mlc.conflicImages.length > 0) || (mlc.conflictBinaries.length > 0)) && (popup == null)) {
				popup = new PopupLibraryConflicts();
				PopUpManager.addPopUp(popup, FlexGlobals.topLevelApplication as DisplayObject, true);
				PopUpManager.centerPopUp(popup);
				mediatorMap.createMediator(popup);
				popup.addEventListener(Event.CLOSE, popupClose);
			}
		}
		private function popupClose(e:Event = null) : void {
			mediatorMap.removeMediatorByView(popup);
			PopUpManager.removePopUp(popup);
			popup = null;
			checkLater()
		}
		
		private function checkLater(e:Event = null) : void {
			if (!checkScheduled){
				checkScheduled = true;
				v.callLater(check);
			}
		}
	}
}